package com.LiL.Wayne.Activity.activity;

import java.io.FileInputStream;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.LiL.Wayne.Activity.R;
import com.LiL.Wayne.Activity.R.anim;
import com.LiL.Wayne.Activity.R.drawable;
import com.LiL.Wayne.Activity.R.id;
import com.LiL.Wayne.Activity.R.layout;
import com.LiL.Wayne.Activity.activity.Videos_Interface.ImageAdapter;
import com.LiL.Wayne.Activity.activity.Videos_Interface.SharePm;
import com.LiL.Wayne.Activity.activity.Videos_Interface.ViewHolder;
import com.audamob.audasingers.model.News;
import com.audamob.audasingers.model.Song;
import com.koushikdutta.urlimageviewhelper.Constants;
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class NewsPage extends Activity {

	String NewsURl;
	String NewsTITLE;
	String NewsTEXT;
	String NewsIMAGEurl;
	String NewsDATE;
	String NewsWRITER;
	News NEWS;

	Boolean PopUpOnshare = false;
	PopupWindow PopUpShare;
	Activity activity;

	class SharePm {
		String Name;
		Drawable d;
		ResolveInfo ResolveInfo;
	}

	int count = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.auda_layout_activity_news_page);
		activity = this;

		Bundle b = getIntent().getExtras();
		this.NewsURl = b.getString("URL");
		this.NewsTITLE = b.getString("TITLE");
		this.NewsTEXT = b.getString("TEXT");
		this.NewsIMAGEurl = b.getString("urlIMAGE");
		this.NewsDATE = b.getString("DATE");
		this.NewsWRITER = b.getString("WRITER");
		NEWS = new News(NewsTITLE, NewsDATE, NewsTEXT, NewsURl, NewsWRITER,
				NewsIMAGEurl);
		ImageView imageArticle, logoWriter;
		TextView titleView, textArticleView, writterView, dateView;
		imageArticle = (ImageView) findViewById(R.id.Image_article);
		logoWriter = (ImageView) findViewById(R.id.writer_logo);
		titleView = (TextView) findViewById(R.id.title);
		textArticleView = (TextView) findViewById(R.id.textArticle);
		writterView = (TextView) findViewById(R.id.writter);
		dateView = (TextView) findViewById(R.id.date);

		dateView.setText(NEWS.getDate());
		writterView.setText(NEWS.getWriter());
		textArticleView.setText(NEWS.getDesccreption());
		titleView.setText(NEWS.getTitle());
		if (NEWS.getWriter().equalsIgnoreCase("MTV News")) {

			if (Build.VERSION.SDK_INT >= 16) {
				logoWriter.setBackground(getResources().getDrawable(
						R.drawable.mtvnews_c));
			} else {
				logoWriter.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.mtvnews_c));
			}
		} else if (NEWS.getWriter().equalsIgnoreCase("MTV Buzzworthy")) {
			if (Build.VERSION.SDK_INT >= 16) {
				logoWriter.setBackground(getResources().getDrawable(
						R.drawable.mtvbuzzworthy_c));
			} else {
				logoWriter.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.mtvbuzzworthy_c));
			}
		} else if (NEWS.getWriter().equalsIgnoreCase("VH1 Tuner")) {
			if (Build.VERSION.SDK_INT >= 16) {
				logoWriter.setBackground(getResources().getDrawable(
						R.drawable.vh1tuner_c));
			} else {
				logoWriter.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.vh1tuner_c));
			}
		}

		UrlImageViewHelper.setUrlDrawable( imageArticle,
				NEWS.getImageUrl());

		ImageView share = (ImageView) findViewById(R.id.Icon_share);
		share.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View vv) {
				// TODO Auto-generated method stub

				if (!PopUpOnshare) {
					PopUpOnshare = true;
					LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					View v = vi.inflate(R.layout.dialog_options_share, null);
					PopUpShare = new PopupWindow(v, 500, 500);
					PopUpShare.setOutsideTouchable(false);
					PopUpShare.showAsDropDown(activity
							.findViewById(R.id.Icon_share));
					ArrayList<SharePm> ListSHare = new ArrayList<SharePm>();
					ListView lv = (ListView) v.findViewById(R.id.ListShare);
					String mimeType = null;
					mimeType = "text/plain";
					Intent shareIntent = new Intent(
							android.content.Intent.ACTION_SEND);
					shareIntent.setType(mimeType);

					shareIntent.putExtra(Intent.EXTRA_TEXT, "");
					shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

					final PackageManager pm = v.getContext()
							.getPackageManager();
					final List<ResolveInfo> activityList = pm
							.queryIntentActivities(shareIntent, 0);
					for (final ResolveInfo app : activityList) {
						SharePm sp = new SharePm();
						sp.d = app.loadIcon(pm);
						sp.Name = app.loadLabel(pm).toString();
						sp.ResolveInfo = app;
						ListSHare.add(sp);
						Log.d("ShareShare", " " + app.loadLabel(pm).toString());
					}
					count = ListSHare.size();
					String nom = "aymennnnn";// ImageList.get(currentIndex).getNewLocation();
					String Nomold = "zvzdv";// ImageList.get(currentIndex).getOldLocation();
					// Nomold=Nomold.substring(Nomold.lastIndexOf("."),
					// Nomold.length());

					lv.setAdapter(new ImageAdapter(ListSHare));

				} else {
					PopUpShare.dismiss();
					PopUpOnshare = false;

				}
			}
		});

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub

		super.onDestroy();
	}

	public class ImageAdapter extends BaseAdapter {
		private LayoutInflater mInflater;
		ArrayList<SharePm> list;

		public ImageAdapter(ArrayList<SharePm> listSHare) {
			mInflater = (LayoutInflater) getBaseContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			list = listSHare;
		}

		public int getCount() {
			return list.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			final ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();

				convertView = mInflater.inflate(R.layout.item_share, null);

				holder.ImageFolder = (ImageView) convertView
						.findViewById(R.id.IconAPp);
				holder.NameFolder = (TextView) convertView
						.findViewById(R.id.namAPp);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					int h = v.getId();
					startResolvedActivity(list.get(holder.id).ResolveInfo);
					// PopUpShare.dismiss();
					// PopUpOnshare=false;
					// hidecurrentImage();
				}
			});

			holder.ImageFolder.setId(position);
			holder.NameFolder.setId(position);

			int h = holder.ImageFolder.getId();

			holder.ImageFolder.setImageDrawable(list.get(h).d);

			holder.NameFolder.setText(list.get(h).Name);
			holder.id = position;
			return convertView;
		}
	}

	class ViewHolder {

		ImageView ImageFolder;
		TextView NameFolder;
		int id;

	}

	private void startResolvedActivity(ResolveInfo info) {

		Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
		String mimeType = null;
		mimeType = "text/plain";
		shareIntent.setType(mimeType);
		ArrayList<Uri> uris = new ArrayList<Uri>();
		String nom = NEWS.getUrl();// ImageList.get(currentIndex).getNewLocation();

		shareIntent.putExtra(Intent.EXTRA_SUBJECT, "@Lil-Wayne-Android");
		shareIntent.putExtra(Intent.EXTRA_TEXT, "@Lil-Wayne-Android  " + nom);
		shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
		final Intent resolvedIntent = new Intent(shareIntent);
		ActivityInfo ai = info.activityInfo;

		resolvedIntent.setComponent(new ComponentName(
				ai.applicationInfo.packageName, ai.name));
		startActivity(resolvedIntent);

	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		if (PopUpOnshare) {
			PopUpOnshare = false;
			PopUpShare.dismiss();
		} else {

			finish();
			overridePendingTransition(R.anim.push_down_out_back,
					R.anim.push_down_in_back);
		}
	}
}
